<?php

namespace app\Modules\Api\controllers;

use Yii;
use app\Modules\Api\models\Push;
use app\filters\Json;
use app\helpers\Request;
use yii\data\Pagination;

/**
 * Class PushController
 * @package app\Modules\Api\controllers
 */
class PushController extends ControllerBase
{
    public function actionIndex()
    {
        $user_id = Request::strictpost('user_id','缺少用户id');
        $query = Push::find()->where(['user_id'=>$user_id]);
        $count = $query->count();
        $pagination = new Pagination([
            'totalCount' => $count,
            'pageSize'=>Yii::$app->request->post('pageSize',6),
            'page'=>Yii::$app->request->post('page'),
        ]);
        $pushs = $query
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->orderBy(['created_at'=>SORT_DESC])
            ->all();
        return Json::success([
            'pushs'=>$pushs,
            'push_count'=>$count,
        ]);
    }

    /**
     * 已读
     * @return array
     */
    public function actionRead()
    {
        $pushid = Request::strictpost('push_id','缺少消息id');
        $model = Push::findOne($pushid);
        $model->state = Push::STATE_READ;
        $model->save(false);
        return Json::success();
    }
}